package com.gz;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@WebServlet(urlPatterns = "/calServlet")
public class CalServlet extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String num1 = request.getParameter("num1");
        String num2 = request.getParameter("num2");
        String oper = request.getParameter("oper");

        Double n1 = Double.parseDouble(num1);
        Double n2 = Double.parseDouble(num2);
        ServletContext servletContext = request.getServletContext();
        if ("+".equals(oper)){
            servletContext.setAttribute("oper","+");
            servletContext.setAttribute("res",n1+n2);
        } else if ("-".equals(oper)) {
            servletContext.setAttribute("oper","-");
            servletContext.setAttribute("res",n1-n2);
        }
        request.getRequestDispatcher("/ok.jsp").forward(request,response);
    }
}
